package com.blb.zy_community.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zy_community.entity.ResponseResult;
import com.blb.zy_community.entity.SysDictType;
import com.blb.zy_community.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 字典类型表 前端控制器
 * </p>
 *
 * @author 二组
 * @since 2023-06-18
 */
@CrossOrigin
@RestController
public class SysDictTypeController {


    @Autowired
    private ISysDictTypeService sysDictTypeService;

    /**
     * 获取字典分页信息
     *
     * @param current              当前页数
     * @param size                 每页几行
     * @param dictName             字典名称
     * @param dictType             字典类型
     * @param status               状态
     * @param createTimeRangeStart 创建起始时间
     * @param createTimeRangeEnd   创建结束时间
     * @return 返回分页数据
     */
    @GetMapping("/sysDictType/page")
    public ResponseResult<Page<SysDictType>> getSysDictTypePage(
            @RequestParam(required = false, defaultValue = "1") Long current,
            @RequestParam(required = false, defaultValue = "5") Long size,
            @RequestParam(required = false) String dictName,
            @RequestParam(required = false) String dictType,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String createTimeRangeStart,
            @RequestParam(required = false) String createTimeRangeEnd

    ) {
        Page<SysDictType> sysDictTypePage = sysDictTypeService.getSysDictTypePage(new Page<>(current, size), dictName, dictType, status, createTimeRangeStart, createTimeRangeEnd);
//        Page<SysDictType> sysDictTypePage1 = sysDictTypeService.page(new Page<>(current, size));
        return ResponseResult.ok(sysDictTypePage);
    }

    /**
     * 添加字典类型
     * @param sysDictType
     * @return
     */
    @PostMapping("/sysDictType")
    public ResponseResult<SysDictType> updateSysDict(@RequestBody SysDictType sysDictType) {
        sysDictType.setCreateTime(LocalDateTime.now());
        sysDictTypeService.save(sysDictType);
        return ResponseResult.ok(sysDictType);
    }

    /**
     * 物理删除菜单类型
     * @param dictId
     * @return
     */
    @DeleteMapping("/sysDict/{dictId}")
    public ResponseResult<Long> deleteSysDict(@PathVariable("dictId") Long dictId) {

        sysDictTypeService.removeById(dictId);
        return ResponseResult.ok(dictId);
    }

    /**
     * 获取全部字典类型
     * @return
     */
    @GetMapping("/sysDictType/List")
    public ResponseResult<List<SysDictType>> getSysDictTypeList() {
        List<SysDictType> list = sysDictTypeService.list();
        return ResponseResult.ok(list);
    }

    /**
     * 批量删除系统字典
     * @param ids
     * @return
     */
    @DeleteMapping("/sysDictType/batchDelete/{ids}")
    public ResponseResult<Long[]> batchDeleteBySysDictTypeIds(@PathVariable("ids") String[] ids) {
        List<String> list = Arrays.asList(ids);
        sysDictTypeService.removeBatchByIds(list);
        return ResponseResult.ok(ids);
    }

    /**
     * 修改字典类型
     * @param sysDictType
     * @return
     */
    @PutMapping("/sysDictType")
    public ResponseResult<SysDictType> updateSysDictType(@RequestBody SysDictType sysDictType) {
        sysDictTypeService.updateById(sysDictType);
        return ResponseResult.ok(sysDictType);
    }


}
